home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / java / io / DataInput.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  18.7 KB  |  495 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)DataInput.java    1.13 98/06/29
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.io;
  16.  
  17. /**
  18.  * The <code>DataInput</code> interface provides
  19.  * for reading bytes from a binary stream and
  20.  * reconstructing from them data in any of
  21.  * the Java primitive types. There is also
  22.  * a
  23.  * facility for reconstructing a <code>String</code>
  24.  * from data in Java modified UTF-8 format.
  25.  * <p>
  26.  * It is generally true of all the reading
  27.  * routines in this interface that if end of
  28.  * file is reached before the desired number
  29.  * of bytes has been read, an <code>EOFException</code>
  30.  * (which is a kind of <code>IOException</code>)
  31.  * is thrown. If any byte cannot be read for
  32.  * any reason other than end of file, an <code>IOException</code>
  33.  * other than <code>EOFException</code> is
  34.  * thrown. In particular, an <code>IOException</code>
  35.  * may be thrown if the input stream has been
  36.  * closed.
  37.  *
  38.  * @author  Frank Yellin
  39.  * @version 1.13, 06/29/98
  40.  * @see     java.io.DataInputStream
  41.  * @see     java.io.DataOutput
  42.  * @since   JDK1.0
  43.  */
  44. public
  45. interface DataInput {
  46.     /**
  47.      * Reads some bytes from an input
  48.      * stream and stores them into the buffer
  49.      * array <code>b</code>. The number of bytes
  50.      * read is equal
  51.      * to the length of <code>b</code>.
  52.      * <p>
  53.      * This method blocks until one of the
  54.      * following conditions occurs:<p>
  55.      * <ul>
  56.      * <li><code>b.length</code>
  57.      * bytes of input data are available, in which
  58.      * case a normal return is made.
  59.      *
  60.      * <li>End of
  61.      * file is detected, in which case an <code>EOFException</code>
  62.      * is thrown.
  63.      *
  64.      * <li>An I/O error occurs, in
  65.      * which case an <code>IOException</code> other
  66.      * than <code>EOFException</code> is thrown.
  67.      * </ul>
  68.      * <p>
  69.      * If <code>b</code> is <code>null</code>,
  70.      * a <code>NullPointerException</code> is thrown.
  71.      * If <code>b.length</code> is zero, then
  72.      * no bytes are read. Otherwise, the first
  73.      * byte read is stored into element <code>b[0]</code>,
  74.      * the next one into <code>b[1]</code>, and
  75.      * so on.
  76.      * If an exception is thrown from
  77.      * this method, then it may be that some but
  78.      * not all bytes of <code>b</code> have been
  79.      * updated with data from the input stream.
  80.      *
  81.      * @param     b   the buffer into which the data is read.
  82.      * @exception  EOFException  if this stream reaches the end before reading
  83.      *               all the bytes.
  84.      * @exception  IOException   if an I/O error occurs.
  85.      */
  86.     void readFully(byte b[]) throws IOException;
  87.  
  88.     /**
  89.      *
  90.      * Reads <code>len</code>
  91.      * bytes from
  92.      * an input stream.
  93.      * <p>
  94.      * This method
  95.      * blocks until one of the following conditions
  96.      * occurs:<p>
  97.      * <ul>
  98.      * <li><code>len</code> bytes
  99.      * of input data are available, in which case
  100.      * a normal return is made.
  101.      *
  102.      * <li>End of file
  103.      * is detected, in which case an <code>EOFException</code>
  104.      * is thrown.
  105.      *
  106.      * <li>An I/O error occurs, in
  107.      * which case an <code>IOException</code> other
  108.      * than <code>EOFException</code> is thrown.
  109.      * </ul>
  110.      * <p>
  111.      * If <code>b</code> is <code>null</code>,
  112.      * a <code>NullPointerException</code> is thrown.
  113.      * If <code>off</code> is negative, or <code>len</code>
  114.      * is negative, or <code>off+len</code> is
  115.      * greater than the length of the array <code>b</code>,
  116.      * then an <code>IndexOutOfBoundsException</code>
  117.      * is thrown.
  118.      * If <code>len</code> is zero,
  119.      * then no bytes are read. Otherwise, the first
  120.      * byte read is stored into element <code>b[off]</code>,
  121.      * the next one into <code>b[off+1]</code>,
  122.      * and so on. The number of bytes read is,
  123.      * at most, equal to <code>len</code>.
  124.      *
  125.      * @param     b   the buffer into which the data is read.
  126.      * @exception  EOFException  if this stream reaches the end before reading
  127.      *               all the bytes.
  128.      * @exception  IOException   if an I/O error occurs.
  129.      */
  130.     void readFully(byte b[], int off, int len) throws IOException;
  131.  
  132.     /**
  133.      * Makes an attempt to skip over
  134.      * <code>n</code> bytes
  135.      * of data from the input
  136.      * stream, discarding the skipped bytes. However,
  137.      * it may skip
  138.      * over some smaller number of
  139.      * bytes, possibly zero. This may result from
  140.      * any of a
  141.      * number of conditions; reaching
  142.      * end of file before <code>n</code> bytes
  143.      * have been skipped is
  144.      * only one possibility.
  145.      * This method never throws an <code>EOFException</code>.
  146.      * The actual
  147.      * number of bytes skipped is returned.
  148.      *
  149.      * @param      n   the number of bytes to be skipped.
  150.      * @return     the number of bytes skipped, which is always <code>n</code>.
  151.      * @exception  EOFException  if this stream reaches the end before skipping
  152.      *               all the bytes.
  153.      * @exception  IOException   if an I/O error occurs.
  154.      */
  155.     int skipBytes(int n) throws IOException;
  156.  
  157.     /**
  158.      * Reads one input byte and returns
  159.      * <code>true</code> if that byte is nonzero,
  160.      * <code>false</code> if that byte is zero.
  161.      * This method is suitable for reading
  162.      * the byte written by the <code>writeBoolean</code>
  163.      * method of interface <code>DataOutput</code>.
  164.      *
  165.      * @return     the <code>boolean</code> value read.
  166.      * @exception  EOFException  if this stream reaches the end before reading
  167.      *               all the bytes.
  168.      * @exception  IOException   if an I/O error occurs.
  169.      */
  170.     boolean readBoolean() throws IOException;
  171.  
  172.     /**
  173.      * Reads and returns one input byte.
  174.      * The byte is treated as a signed value in
  175.      * the range <code>-128</code> through <code>127</code>,
  176.      * inclusive.
  177.      * This method is suitable for
  178.      * reading the byte written by the <code>writeByte</code>
  179.      * method of interface <code>DataOutput</code>.
  180.      *
  181.      * @return     the 8-bit value read.
  182.      * @exception  EOFException  if this stream reaches the end before reading
  183.      *               all the bytes.
  184.      * @exception  IOException   if an I/O error occurs.
  185.      */
  186.     byte readByte() throws IOException;
  187.  
  188.     /**
  189.      * Reads one input byte, zero-extends
  190.      * it to type <code>int</code>, and returns
  191.      * the result, which is therefore in the range
  192.      * <code>0</code>
  193.      * through <code>255</code>.
  194.      * This method is suitable for reading
  195.      * the byte written by the <code>writeByte</code>
  196.      * method of interface <code>DataOutput</code>
  197.      * if the argument to <code>writeByte</code>
  198.      * was intended to be a value in the range
  199.      * <code>0</code> through <code>255</code>.
  200.      *
  201.      * @return     the unsigned 8-bit value read.
  202.      * @exception  EOFException  if this stream reaches the end before reading
  203.      *               all the bytes.
  204.      * @exception  IOException   if an I/O error occurs.
  205.      */
  206.     int readUnsignedByte() throws IOException;
  207.  
  208.     /**
  209.      * Reads two input bytes and returns
  210.      * a <code>short</code> value. Let <code>a</code>
  211.      * be the first byte read and <code>b</code>
  212.      * be the second byte. The value
  213.      * returned
  214.      * is:
  215.      * <p><pre><code>(short)((a << 8) * | (b & 0xff))
  216.      * </code></pre>
  217.      * This method
  218.      * is suitable for reading the bytes written
  219.      * by the <code>writeShort</code> method of
  220.      * interface <code>DataOutput</code>.
  221.      *
  222.      * @return     the 16-bit value read.
  223.      * @exception  EOFException  if this stream reaches the end before reading
  224.      *               all the bytes.
  225.      * @exception  IOException   if an I/O error occurs.
  226.      */
  227.     short readShort() throws IOException;
  228.  
  229.     /**
  230.      * Reads two input bytes and returns
  231.      * an <code>int</code> value in the range <code>0</code>
  232.      * through <code>65535</code>. Let <code>a</code>
  233.      * be the first byte read and
  234.      * <code>b</code>
  235.      * be the second byte. The value returned is:
  236.      * <p><pre><code>(((a & 0xff) << 8) | (b & 0xff))
  237.      * </code></pre>
  238.      * This method is suitable for reading the bytes
  239.      * written by the <code>writeShort</code> method
  240.      * of interface <code>DataOutput</code>  if
  241.      * the argument to <code>writeShort</code>
  242.      * was intended to be a value in the range
  243.      * <code>0</code> through <code>65535</code>.
  244.      *
  245.      * @return     the unsigned 16-bit value read.
  246.      * @exception  EOFException  if this stream reaches the end before reading
  247.      *               all the bytes.
  248.      * @exception  IOException   if an I/O error occurs.
  249.      */
  250.     int readUnsignedShort() throws IOException;
  251.  
  252.     /**
  253.      * Reads an input <code>char</code> and returns the <code>char</code> value.
  254.      * A Unicode <code>char</code> is made up of two bytes.
  255.      * Let <code>a</code>
  256.      * be the first byte read and <code>b</code>
  257.      * be the second byte. The value
  258.      * returned is:
  259.      * <p><pre><code>(char)((a << 8) | (b & 0xff))
  260.      * </code></pre>
  261.      * This method
  262.      * is suitable for reading bytes written by
  263.      * the <code>writeChar</code> method of interface
  264.      * <code>DataOutput</code>.
  265.      *
  266.      * @return     the Unicode <code>char</code> read.
  267.      * @exception  EOFException  if this stream reaches the end before reading
  268.      *               all the bytes.
  269.      * @exception  IOException   if an I/O error occurs.
  270.      */
  271.     char readChar() throws IOException;
  272.  
  273.     /**
  274.      * Reads four input bytes and returns an
  275.      * <code>int</code> value. Let <code>a</code>
  276.      * be the first byte read, <code>b</code> be
  277.      * the second byte, <code>c</code> be the third
  278.      * byte,
  279.      * and <code>d</code> be the fourth
  280.      * byte. The value returned is:
  281.      * <p><pre>
  282.      * <code>
  283.      * (((a & 0xff) << 24) | ((b & 0xff) << 16) |
  284.      *  ((c & 0xff) << 8) | (d & 0xff))
  285.      * </code></pre>
  286.      * This method is suitable
  287.      * for reading bytes written by the <code>writeInt</code>
  288.      * method of interface <code>DataOutput</code>.
  289.      *
  290.      * @return     the <code>int</code> value read.
  291.      * @exception  EOFException  if this stream reaches the end before reading
  292.      *               all the bytes.
  293.      * @exception  IOException   if an I/O error occurs.
  294.      */
  295.     int readInt() throws IOException;
  296.  
  297.     /**
  298.      * Reads eight input bytes and returns
  299.      * a <code>long</code> value. Let <code>a</code>
  300.      * be the first byte read, <code>b</code> be
  301.      * the second byte, <code>c</code> be the third
  302.      * byte, <code>d</code>
  303.      * be the fourth byte,
  304.      * <code>e</code> be the fifth byte, <code>f</code>
  305.      * be the sixth byte, <code>g</code> be the
  306.      * seventh byte,
  307.      * and <code>h</code> be the
  308.      * eighth byte. The value returned is:
  309.      * <p><pre> <code>
  310.      * (((long)(a & 0xff) << 56) |
  311.      *  ((long)(b & 0xff) << 48) |
  312.      *  ((long)(c & 0xff) << 40) |
  313.      *  ((long)(d & 0xff) << 32) |
  314.      *  ((long)(e & 0xff) << 24) |
  315.      *  ((long)(f & 0xff) << 16) |
  316.      *  ((long)(g & 0xff) <<  8) |
  317.      *  ((long)(h & 0xff)))
  318.      * </code></pre>
  319.      * <p>
  320.      * This method is suitable
  321.      * for reading bytes written by the <code>writeLong</code>
  322.      * method of interface <code>DataOutput</code>.
  323.      *
  324.      * @return     the <code>long</code> value read.
  325.      * @exception  EOFException  if this stream reaches the end before reading
  326.      *               all the bytes.
  327.      * @exception  IOException   if an I/O error occurs.
  328.      */
  329.     long readLong() throws IOException;
  330.  
  331.     /**
  332.      * Reads four input bytes and returns
  333.      * a <code>float</code> value. It does this
  334.      * by first constructing an <code>int</code>
  335.      * value in exactly the manner
  336.      * of the <code>readInt</code>
  337.      * method, then converting this <code>int</code>
  338.      * value to a <code>float</code> in
  339.      * exactly the manner of the method <code>Float.intBitsToFloat</code>.
  340.      * This method is suitable for reading
  341.      * bytes written by the <code>writeFloat</code>
  342.      * method of interface <code>DataOutput</code>.
  343.      *
  344.      * @return     the <code>float</code> value read.
  345.      * @exception  EOFException  if this stream reaches the end before reading
  346.      *               all the bytes.
  347.      * @exception  IOException   if an I/O error occurs.
  348.      */
  349.     float readFloat() throws IOException;
  350.  
  351.     /**
  352.      * Reads eight input bytes and returns
  353.      * a <code>double</code> value. It does this
  354.      * by first constructing a <code>long</code>
  355.      * value in exactly the manner
  356.      * of the <code>readlong</code>
  357.      * method, then converting this <code>long</code>
  358.      * value to a <code>double</code> in exactly
  359.      * the manner of the method <code>Double.longBitsToDouble</code>.
  360.      * This method is suitable for reading
  361.      * bytes written by the <code>writeDouble</code>
  362.      * method of interface <code>DataOutput</code>.
  363.      *
  364.      * @return     the <code>double</code> value read.
  365.      * @exception  EOFException  if this stream reaches the end before reading
  366.      *               all the bytes.
  367.      * @exception  IOException   if an I/O error occurs.
  368.      */
  369.     double readDouble() throws IOException;
  370.  
  371.     /**
  372.      * Reads the next line of text from the input stream.
  373.      * It reads successive bytes, converting
  374.      * each byte separately into a character,
  375.      * until it encounters a line terminator or
  376.      * end of
  377.      * file; the characters read are then
  378.      * returned as a <code>String</code>. Note
  379.      * that because this
  380.      * method processes bytes,
  381.      * it does not support input of the full Unicode
  382.      * character set.
  383.      * <p>
  384.      * If end of file is encountered
  385.      * before even one byte can be read, then <code>null</code>
  386.      * is returned. Otherwise, each byte that is
  387.      * read is converted to type <code>char</code>
  388.      * by zero-extension. If the character <code>'\n'</code>
  389.      * is encountered, it is discarded and reading
  390.      * ceases. If the character <code>'\r'</code>
  391.      * is encountered, it is discarded and, if
  392.      * the following byte converts  to the
  393.      * character <code>'\n'</code>, then that is
  394.      * discarded also; reading then ceases. If
  395.      * end of file is encountered before either
  396.      * of the characters <code>'\n'</code> and
  397.      * <code>'\r'</code> is encountered, reading
  398.      * ceases. Once reading has ceased, a <code>String</code>
  399.      * is returned that contains all the characters
  400.      * read and not discarded, taken in order.
  401.      * Note that every character in this string
  402.      * will have a value less than <code>\u0100</code>,
  403.      * that is, <code>(char)256</code>.
  404.      *
  405.      * @return     if this stream reaches the end before reading all the bytes.
  406.      * @exception  IOException  if an I/O error occurs.
  407.      */
  408.     String readLine() throws IOException;
  409.  
  410.     /**
  411.      * Reads in a string that has been encoded using a modified UTF-8 format.
  412.      * The general contract of <code>readUTF</code>
  413.      * is that it reads a representation of a Unicode
  414.      * character string encoded in Java modified
  415.      * UTF-8 format; this string of characters
  416.      * is then returned as a <code>String</code>.
  417.      * <p>
  418.      * First, two bytes are read and used to
  419.      * construct an unsigned 16-bit integer in
  420.      * exactly the manner of the <code>readUnsignedShort</code>
  421.      * method . This integer value is called the
  422.      * <i>UTF length</i> and specifies the number
  423.      * of additional bytes to be read. These bytes
  424.      * are then converted to characters by considering
  425.      * them in groups. The length of each group
  426.      * is computed from the value of the first
  427.      * byte of the group. The byte following a
  428.      * group, if any, is the first byte of the
  429.      * next group.
  430.      * <p>
  431.      * If the first byte of a group
  432.      * matches the bit pattern <code>0xxxxxxx</code>
  433.      * (where <code>x</code> means "may be <code>0</code>
  434.      * or <code>1</code>"), then the group consists
  435.      * of just that byte. The byte is zero-extended
  436.      * to form a character.
  437.      * <p>
  438.      * If the first byte
  439.      * of a group matches the bit pattern <code>110xxxxx</code>,
  440.      * then the group consists of that byte <code>a</code>
  441.      * and a second byte <code>b</code>. If there
  442.      * is no byte <code>b</code> (because byte
  443.      * <code>a</code> was the last of the bytes
  444.      * to be read), or if byte <code>b</code> does
  445.      * not match the bit pattern <code>10xxxxxx</code>,
  446.      * then a <code>UTFDataFormatException</code>
  447.      * is thrown. Otherwise, the group is converted
  448.      * to the character:<p>
  449.      * <pre><code>(char)(((a& 0x1F) << 6) | (b & 0x3F))
  450.      * </code></pre>
  451.      * If the first byte of a group
  452.      * matches the bit pattern <code>1110xxxx</code>,
  453.      * then the group consists of that byte <code>a</code>
  454.      * and two more bytes <code>b</code> and <code>c</code>.
  455.      * If there is no byte <code>c</code> (because
  456.      * byte <code>a</code> was one of the last
  457.      * two of the bytes to be read), or either
  458.      * byte <code>b</code> or byte <code>c</code>
  459.      * does not match the bit pattern <code>10xxxxxx</code>,
  460.      * then a <code>UTFDataFormatException</code>
  461.      * is thrown. Otherwise, the group is converted
  462.      * to the character:<p>
  463.      * <pre><code>
  464.      * (char)(((a & 0x0F) << 12) | ((b & 0x3F) << 6) | (c & 0x3F))
  465.      * </code></pre>
  466.      * If the first byte of a group matches the
  467.      * pattern <code>1111xxxx</code> or the pattern
  468.      * <code>10xxxxxx</code>, then a <code>UTFDataFormatException</code>
  469.      * is thrown.
  470.      * <p>
  471.      * If end of file is encountered
  472.      * at any time during this entire process,
  473.      * then an <code>EOFException</code> is thrown.
  474.      * <p>
  475.      * After every group has been converted to
  476.      * a character by this process, the characters
  477.      * are gathered, in the same order in which
  478.      * their corresponding groups were read from
  479.      * the input stream, to form a <code>String</code>,
  480.      * which is returned.
  481.      * <p>
  482.      * The <code>writeUTF</code>
  483.      * method of interface <code>DataOutput</code>
  484.      * may be used to write data that is suitable
  485.      * for reading by this method.
  486.      * @return     a Unicode string.
  487.      * @exception  EOFException            if this stream reaches the end
  488.      *               before reading all the bytes.
  489.      * @exception  IOException             if an I/O error occurs.
  490.      * @exception  UTFDataFormatException  if the bytes do not represent a
  491.      *               valid UTF-8 encoding of a string.
  492.      */
  493.     String readUTF() throws IOException;
  494. }
  495.